<template>
  <div>
    <a-modal centered :footer="null" v-model="visible" title="" on-ok="handleOk">
      <div class="top" style="text-align: center; margin-bottom: 10px">
        <div class="header">
          <img width="32" src="@/assets/img/logo.png" style="margin-right: 10px" />
          <span class="title">{{systemName}}</span>
        </div>
        <div class="desc" style="padding-top: 20px">校园二手交易平台</div>
      </div>
      <div class="login">
        <a-tabs size="large" :tabBarStyle="{textAlign: 'center'}" @change="callback" style="padding: 0 2px;">
          <a-tab-pane tab="手机号密码登录" key="1">
            <a-form @submit="submitLoginForm" :form="loginForm">
              <a-alert type="error" :closable="true" v-if="error" :message="error" @close='onClose' showIcon style="margin-bottom: 24px;" />
              <a-form-item>
                <a-input
                    autocomplete="autocomplete"
                    size="large"
                    placeholder="手机号"
                    v-decorator="[
                        'phone', {rules:
                        [{ required: true, message: '请输入手机号', whitespace: true}]}]"
                >
                  <a-icon slot="prefix" type="user" />
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-input
                    size="large"
                    placeholder="密码"
                    autocomplete="autocomplete"
                    type="password"
                    v-decorator="['password', {rules: [{ required: true, message: '请输入密码', whitespace: true}]}]"
                >
                  <a-icon slot="prefix" type="lock" />
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-button :loading="loginLogging" style="width: 100%;margin-top: 24px" size="large" htmlType="submit" type="primary">登录</a-button>
              </a-form-item>
            </a-form>
          </a-tab-pane>
          <a-tab-pane tab="注册" key="2">
            <a-form @submit="submitRegisterForm" :form="registerForm">
              <a-alert type="error" :closable="true" v-if="error" :message="error" @close='onClose' showIcon style="margin-bottom: 24px;" />
              <a-form-item>
                <a-input
                    autocomplete="autocomplete"
                    size="large"
                    placeholder="手机号"
                    v-decorator="['phone', {rules: [{ required: true, message: '请输入手机号', whitespace: true}]}]"
                >
                  <a-icon slot="prefix" type="user" />
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-input
                    size="large"
                    placeholder="密码"
                    autocomplete="autocomplete"
                    type="password"
                    v-decorator="['password', {rules: [{ required: true, message: '请输入密码', whitespace: true}]}]"
                >
                  <a-icon slot="prefix" type="lock" />
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-input
                    size="large"
                    placeholder="确认密码"
                    autocomplete="autocomplete"
                    type="password"
                    v-decorator="['verifyPassword',{rules: [
                        {required: true, message: '请再次输入密码',},
                        {validator: compareToFirstPassword,},],},]"
                >
                  <a-icon slot="prefix" type="lock" />
                </a-input>
              </a-form-item>
              <a-form-item>
                <a-button :loading="registerLogging" style="width: 100%;margin-top: 24px" size="large" htmlType="submit" type="primary">注册</a-button>
              </a-form-item>
            </a-form>
          </a-tab-pane>
        </a-tabs>
      </div>
    </a-modal>
  </div>
</template>

<script>

// import {login} from "@/services/user";
// import {mapMutations} from "vuex";

export default {
  name: "UserLoginModal",
  data() {
    return {
      visible: false,
      loginLogging: false,
      registerLogging: false,
      error: '',
      loginForm: this.$form.createForm(this),
      registerForm: this.$form.createForm(this)
    }
  },
  computed: {
    systemName () {
      return this.$store.state.setting.systemName
    }
  },
  methods: {
    // ...mapMutations('user', ['setUser', 'setToken']),
    showModal() {
      this.visible = true;
    },
    resetModal() {
      // 重置表单信息
      this.loginForm.resetFields()
      this.loginLogging = false
      this.registerForm.resetFields()
      this.registerLogging = false
    },
    // 表单确认密码校验
    compareToFirstPassword(rule, value, callback) {
      const form = this.registerForm;
      if (value && value !== form.getFieldValue('password')) {
        callback('两次输入密码不一致');
      } else {
        callback();
      }
    },
    submitLoginForm (e) {
      e.preventDefault()
      this.loginForm.validateFields((err) => {
        if (!err) {
          this.loginLogging = true
          // 获取表单参数
          const phone = this.loginForm.getFieldValue('phone')
          const password = this.loginForm.getFieldValue('password')
          console.log(phone, ' ', password)
          // TODO 登录请求

        }
      })
    },
    submitRegisterForm (e) {
      e.preventDefault()
      this.registerForm.validateFields((err) => {
        if (!err) {
          this.registerLogging = true
          // 获取表单参数
          const phone = this.registerForm.getFieldValue('phone')
          const password = this.registerForm.getFieldValue('password')
          const verifyPassword = this.registerForm.getFieldValue('verifyPassword')
          console.log(phone, ' ', password, ' ', verifyPassword)
          // TODO 注册请求
        }
      })
    },
    // 切换标签页回调
    callback(key) {
      console.log(key)
      if (key === '1') { // 重置注册页面信息
        this.registerLogging = false
        this.registerForm.resetFields()
      } else if (key === '2') { //重置登录页面信息
        this.loginLogging = false
        this.loginForm.resetFields()
      }
    },
    onClose() {
      this.error = false
    },
  }
}
</script>

<style lang="less" scoped>
  .title {
    font-size: 33px;
    color: @title-color;
    font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
    font-weight: 600;
    position: relative;
    top: 2px;
  }
</style>
